public class Singleton {

    private static volatile Singleton singleton = null;

    private Singleton() {
    }

    // 双重检查锁
    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }

    public static void main(String[] args) {
        Singleton2.getInstance().play();
    }

}

class Singleton2 {
    private static class SingletonHolder {
        private static Singleton2 singleton = new Singleton2();
    }

    private Singleton2() {
    }

    public static Singleton2 getInstance() {
        return SingletonHolder.singleton;
    }

    void play(){
        System.out.println("Singleton2");
    }
}
